package com.zby.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.admin.pojo.Admin;
import com.zby.admin.pojo.dto.AdminDto;
import com.zby.admin.pojo.dto.UserDto;
import com.zby.admin.pojo.LoginResult;
import com.zby.admin.pojo.vo.UserVo;
import com.zby.admin.service.UserService;
import com.zby.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 注册用户信息
     *
     * @param userVo
     * @return
     */
    @PostMapping("/register")
    public Result<UserDto> register(@RequestBody UserDto userVo) {
        if (userVo.getUserName() == null ) {
            return Result.error(500, "用户名不能为空");
        }
        if (userVo.getUserNickname() == null ) {
            return Result.error(500, "用户昵称不能为空");
        }
        if (userVo.getUserPassword() == null ) {
            return Result.error(500, "用户密码不能为空");
        }
        boolean save = userService.insertData(userVo);
        if (!save) {
            return Result.error(500, "注册失败");
        }
        return Result.success();

    }

    /**
     * 登录
     *
     * @param admin
     * @return
     */
    @PostMapping("/login")
    public Result<LoginResult> login(@RequestBody AdminDto admin, HttpServletRequest request) {
        LoginResult loginResult = userService.login(admin, request);
        if (loginResult == null) {
            return Result.error(500, "登录失败");
        }
        // 管理员登录返回200，普通用户登录返回100
        if (loginResult.isDetermineRole()) {
            return Result.success(200,loginResult);
        } else {
            return Result.success(100,loginResult);
        }
    }

    /**
     * 获取管理员用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getAdminInfo")
    public Result<Admin> getAdminInfo(@RequestParam Integer id) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Admin admin = userService.getOne(queryWrapper);
        if (admin == null) {
            return Result.error(500, "获取用户信息失败");
        }
        return Result.success(admin);
    }

    /**
     * 修改管理员信息
     *
     * @param
     * @return
     */
    @PutMapping("/updateAdminInfo")
    public Result<Admin> updateAdminInfo(@RequestBody AdminDto adminDto, HttpServletRequest request) {

        boolean  update = userService.updateAdminInfo(adminDto,request);
        if (!update) {
            return Result.error(500, "修改用户信息失败");
        }
        return Result.success();
    }

    /**
     * 获取用户列表
     *
     * @param
     * @return
     */
    @RequestMapping("/getAdminList")
    public Result<Page<Admin>> getAdminList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Admin> page1 = userService.selectPage(page, pageSize);
        return Result.success(page1);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAdmin")
    public Result<Integer> deleteAdmin(@RequestParam Integer id) {
        boolean b = userService.removeById(id);
        if (b) {
            return Result.success();
        }
        return Result.error(500, "删除用户失败");
    }
    /**
     * 添加用户(管理员操作)
     */
    @PostMapping("/addAdmin")
    public Result<Admin> addAdmin(@RequestBody AdminDto adminDto, HttpServletRequest request) {
        System.out.println(adminDto);
        boolean admin = userService.insertSave(adminDto, request);
        if (admin) {
            return Result.success();
        }

        return Result.error(500, "添加用户失败");
    }
    /**
     * 获取用户列表
     */
    @GetMapping("/getUserList")
    public Result<Page<UserVo>> getUserList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<UserVo> page1 = userService.selectUserPage(page, pageSize);
        return Result.success(page1);

    }
    /**
     * 获取个人用户信息
     * 
     * */
    @GetMapping("/getUserInfo")
    public Result<UserVo> getUserInfo(HttpServletRequest request) {
        UserVo userVo = userService.getUserInfo(request);
        if (userVo == null) {
            return Result.error(500, "获取用户信息失败");
        }
        return Result.success(userVo);
    }
    /**
     * 修改用户信息
     *
     * */
    @PutMapping("/updateUserInfo")
    public Result<UserVo> updateUserInfo(@RequestBody UserDto userDto, HttpServletRequest request) {
        boolean update = userService.updateUserInfo(userDto, request);
        if (update) {
            return Result.success();
        }
        return Result.error(500, "修改用户信息失败");
    }
    /**
     * 删除用户
     *
     * */
    @DeleteMapping("/deleteUser")
    public Result<Integer> deleteUser(@RequestParam Integer id) {

        return userService.removeUserById(id);
    }
    /**
     * 添加用户个人信息
     *
     * */
    @PostMapping("/addUser")
    public Result<UserVo> addUser(@RequestBody UserDto userDto) {
        boolean save = userService.insertData(userDto);
        if (save) {
            return Result.success();
        }
        return Result.error(500, "添加用户失败");
    }
    /**
     * 获取用户详细信息，根据用户id
     *
     * */
    @GetMapping("/getUserInfoById")
    public Result<UserVo> getUserInfoById(@RequestParam Integer id) {

        return userService.getUserInfoById(id);
    }


}